package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lv.common.bean.FactoryBorderBeans
import com.lv.common.imp.OnPositionMore
import com.lv.common.imp.OnPositionTListener
import com.lv.service.R
import com.lv.service.adapter.FactoryBorderAdapter
import com.lxj.xpopup.core.BottomPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述
 */
class BorderPopupView(
    private val mContext: Context,
    private val mAllBeans: List<FactoryBorderBeans>? = null,
    private val mTitle: String,
    private val mTopH:Int=0,
    private val mPositionTListener: OnPositionMore<List<FactoryBorderBeans>>? = null
) :
    BottomPopupView(mContext) {
    private var current = 0
    private var currentChild = 0
    private val name = ""
    private var rvContent: RecyclerView? = null
    private var tvTitle: TextView? = null
    private var viewTop: View? = null
    private var viewShadow: TextView? = null
    private val mBeans = mutableListOf<FactoryBorderBeans>()

    private var mAdapter: FactoryBorderAdapter? = null
    override fun getImplLayoutId(): Int {
        return R.layout.recycler_selected_tag_1_popup
    }

    override fun onCreate() {
        super.onCreate()
        rvContent = findViewById<RecyclerView>(R.id.rv_content)
        tvTitle = findViewById<TextView>(R.id.tv_title)
        viewTop = findViewById<View>(R.id.view_top)
        viewShadow = findViewById<TextView>(R.id.tv_submit)
        findViewById<ImageView>(R.id.iv_close).setOnClickListener { dismiss() }
        val layoutParams = viewTop?.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.height = mTopH
        initView()
        initEvnet()
    }

    private fun initView() {
        mAllBeans?.let {
            mBeans.clear()
//            for (i in 0 until mAllBeans.size) {
//                val factoryBorderBeans = mAllBeans[i].imageList
//                factoryBorderBeans?.let {
//                    for (j in 0 until factoryBorderBeans.size) {
//                        val factoryBorderItemBean = factoryBorderBeans[j]
//                        if (factoryBorderItemBean.selected) {
//                            current = i
//                            currentChild = j
//                        }
//                    }
//                }
//            }
            mAllBeans.forEachIndexed { index, commonSelectedBean ->
                commonSelectedBean.imageList?.forEachIndexed { indexChild, factoryBorderItemBean ->
                    if (factoryBorderItemBean.selected_android == true) {
                        current = index
                        currentChild = indexChild
                    }
                }
            }
            mBeans.addAll(mAllBeans)
        }
        tvTitle?.text = mTitle

        val mLinearLayoutManager =
            LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
        rvContent?.layoutManager = mLinearLayoutManager
        mAdapter = FactoryBorderAdapter(mBeans)
        rvContent?.adapter = mAdapter
        mAdapter?.onViewClickListener = object : OnPositionMore<FactoryBorderBeans> {
            override fun onClick(vararg args: Int, data: FactoryBorderBeans?) {
                val parentPosition = args[0]
                val childPosition = args[1]
                val selected =
                    mBeans[parentPosition].imageList?.get(childPosition)?.selected_android ?: false
                if (!selected) {
                    mBeans.forEach { item ->
                        val imageList = item.imageList
                        imageList?.forEach { child ->
                            child.selected_android = false
                        }
                    }
                    mBeans[parentPosition].imageList?.get(childPosition)?.selected_android =
                        !selected
                    mAdapter?.notifyDataSetChanged()
                }
                current = parentPosition
                currentChild = childPosition
//                showToast("$parentPosition==$childPosition")
            }
        }
//        mAdapter?.onViewItemClickListener = OnPositionTListener { position, data ->
//            if (current == position) {
//                return@OnPositionTListener
//            }
//            mBeans[current].selected = false
//            mAdapter?.notifyItemChanged(current)
//            current = position
//            mBeans[current].selected = true
//            mAdapter?.notifyItemChanged(current)
//            mPositionTListener?.onClick(current, mBeans[current])
//            dismiss()
//        }

    }

    private fun initEvnet() {

        viewTop?.setOnClickListener {

            dismiss()
        }

        viewShadow?.setOnClickListener {
            mPositionTListener?.onClick(current, currentChild, data = mBeans)
            dismiss()
        }
    }

    private fun setDismissDialog() {
        dismiss()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()


    }

}